//
//  BADMenuViewController.m
//  BAD
//
//  Created by Can on 2018/5/14.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "BADMenuViewController.h"
#import "BADListViewController.h"

static NSOperationQueue *queue;

@interface BADMenuViewController ()

@property (nonatomic, assign) BOOL listType;

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation BADMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];

    queue=[[NSOperationQueue alloc]init];

    NSInvocationOperation *op=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadWeb) object:nil];

    [queue addOperation:op];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    
    self.listType = YES;
    
    //注册通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(itemAction:) name:@"ItemDidClickNotification" object:nil];
}

- (void)creatView
{
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
    [_webView setBackgroundColor:[UIColor clearColor]];
    [_webView setOpaque:NO];
    _webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];
}

-(void)downLoadWeb
{
    NSURL *url=[NSURL URLWithString:@"http://www.maxigenes.com.au"];
    NSError *error;
    NSString *strData=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    NSData *data=[strData dataUsingEncoding:NSUTF8StringEncoding];
    if (data !=nil) {
        [self performSelectorOnMainThread:@selector(downLoad_completed:) withObject:data waitUntilDone:NO];
    }else{
        NSLog(@"error when download:%@",error);
    }
}

-(void)downLoad_completed:(NSData *)data
{
    NSURL *url=[NSURL URLWithString:@"http://www.maxigenes.com.au"];
    NSString *nameType=[self mimeType:url];
    [_webView loadData:data MIMEType:nameType textEncodingName:@"UTF-8" baseURL:url];
}

#pragma mark 获取指定URL的MIMEType类型
- (NSString *)mimeType:(NSURL *)url
{
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //使用同步方法后去MIMEType
    NSURLResponse *response = nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    return response.MIMEType;
}

#pragma mark - 通知的方法
-(void)itemAction:(NSNotification *)notification{
    if (_listType) {
        [self.navigationController pushViewController:[BADListViewController new] animated:YES];
        _listType = NO;
    }
}

- (void)dealloc
{
    if (_webView) {
        [_webView stopLoading];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
